<script type="text/javascript" src="{{ getAssets }}js/editor/wangEditor.min.js"></script>
<script src="{{ getAssets }}js/ajaxfileupload.js?v={{ assets_version }}"></script>
<script>
//编辑器
var E = window.wangEditor
var editor = new E('#editor');
editor.customConfig.uploadImgServer = '{{ urlfor "WechatController.EditorUpload" }}';
editor.customConfig.uploadFileName = 'inputImageUrl';
editor.customConfig.uploadImgMaxLength = 1;//每次只上传一个
editor.customConfig.uploadImgHooks = {
	customInsert: function (insertImg, result, editor) {
		if(result.code == 1) {
			layer.msg(result.msg);	
			setTimeout(function(){
				layer.closeAll()	
			}, 2000)
		}
        var url = result.data
        insertImg(url)
    }
};
editor.customConfig.onchange = function (html) {
   $('#content').val(html);
}
editor.create();

$('#inputImageUrl').on('change', function() {
      updloadCtrolInit();
});
function updloadCtrolInit() {
	xsrf = $('meta[name="csrf-token"]').attr('content')
	var $this = $('#inputImageUrl');
	var fileinput = $("input[type='file']", this);
	// file 控件的id
	var inputId = $this.attr('id');

	//获取隐藏用于保存图片地址的input
	$.ajaxFileUpload({
		url: '{{ urlfor "WechatController.EditorUpload" }}',
		secureuri: false,
		fileElementId: inputId,
		type: 'post',   //当要提交自定义参数时，这个参数要设置成post
		dataType: 'json',
		data: { type: 1 ,'_xsrf': xsrf}, //用来传file其他的值
		success: function (re, status) {
			if (re.code === 0) {
				fileNames = '<input type="hidden" name="Image" value="'+ re.data +'" /><span class="am-badge am-badge-success">' + re.data + '</span> <a class="am-badge am-badge-primary" href="'+ re.data +'" target="_blank">查看</a>';			
				$('#file-list').html(fileNames);
			}else {
				layer.alert(re.msg, { icon: 2, title: '错误' });
			}
		},
		error: function (XHR, status, e) {              
		}
	});
}	
$('.radios').change(function(){
	var val = $('input[name="Typeid"]:checked').val();
	if(val == 1) {
		$('#content-text').show();
		$('#content-url').hide();
	} else {
		$('#content-text').hide();
		$('#content-url').show();
	}
});
$('.radios').change();
</script>